package none.rg.gantt.web;


import none.rg.gantt.xmlio.*;
import none.rg.gantt.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;


public class GanttServlet extends HttpServlet {
    
    
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        
        req.setAttribute("chart", "No chart loaded");
        getServletContext().getRequestDispatcher("/WEB-INF/template.jsp").include(req, resp);
        
    } // doGet
    
    
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = req.getReader();
        String line;
        
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            sb.append('\n');
        } // while
        
        reader.close();
        
        Chart chart = ChartLoader.getInstance().parseText(sb.toString());
        
        if (chart == null) {
            resp.getWriter().println("<b>Chart description parsing error</b>");
            return;
        } // if

        if (!chart.isCalculable()) {
            resp.getWriter().println("<b>Chart is not calculable due to internal loop dependencies</b>");
            return;
        } // if
        
        req.setAttribute("chart", chart);
        req.setAttribute("scale", 45);
        try {
        getServletContext().getRequestDispatcher("/WEB-INF/chart.jsp").include(req, resp);
        } catch (Exception e) {
            resp.getWriter().println("Error: " + e);
        } // catch
    } // doPost
    
    
} // class GanttServlet

